package com.ywjt.platformbusiness.store.order.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.order.adapter.OrderListChild_Adapter;
import com.ywjt.platformbusiness.store.activity.CheckingCommentActivity;
import com.ywjt.platformbusiness.store.order.model.StoreOrderListModel;
import com.ywjt.platformbusiness.widget.CallPhoneBottomDialog;


public class StoreOrderFinish extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    private TextView tvCheckingComment,tvCopy,tvName,tvPhone,tvAddress,tvOrdernum,tvOrderCreateTime,
            tvPayTime,tvPayType,tvReMark,tvCallBuyer,tvColoudGold,tvGoodsMoney,tvTotalMoney;
    private RecyclerView rcView;
    private StoreOrderListModel.ResultDTO.RecordsDTO model;
    private OrderListChild_Adapter adapter;

    public static void actionStart(Context context,StoreOrderListModel.ResultDTO.RecordsDTO model) {
        Intent intent = new Intent(context, StoreOrderFinish.class);
        intent.putExtra("model",model);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvCheckingComment:
                CheckingCommentActivity.actionStart(this);
                break;
            case R.id.tvCallBuyer:
                //联系我们
                CallPhoneBottomDialog dialog=new CallPhoneBottomDialog(getContext());
                dialog.show(getSupportFragmentManager());
                break;
            case R.id.tvCopy:
                //复制订单号到剪切板
                copyText();
                break;
        }
    }
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void wxPaySuccess(EventBusBean eventBus){
//        switch (eventBus.getCode()){
//            case 78://课程目录点击事件
//                cancelOrder(eventBus.getMsg());
//                break;
//        }
//    }


    //实现复制订单号到剪切板
    public void copyText(){
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("orderNUm",tvOrdernum.getText().toString().trim());
// 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        Toast.makeText(StoreOrderFinish.this,"已成功复制订单号到剪切板",Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.storeorde_finish;
    }

    @Override
    protected void initView() {
//        if(!EventBus.getDefault().isRegistered(this)){//加上判断
//            EventBus.getDefault().register(this);
//        }
        model=(StoreOrderListModel.ResultDTO.RecordsDTO)getIntent().getSerializableExtra("model");
        ivBack=findViewById(R.id.ivBack);
        tvCopy=findViewById(R.id.tvCopy);
        tvCheckingComment=findViewById(R.id.tvCheckingComment);
        tvPayType=findViewById(R.id.tvPayType);
        tvGoodsMoney=findViewById(R.id.tvGoodsMoney);
        tvColoudGold=findViewById(R.id.tvColoudGold);
        tvPhone=findViewById(R.id.tvPhone);
        tvCallBuyer=findViewById(R.id.tvCallBuyer);
        tvPayTime=findViewById(R.id.tvPayTime);
        tvCopy=findViewById(R.id.tvCopy);
        rcView=findViewById(R.id.rcView);
        tvTotalMoney=findViewById(R.id.tvTotalMoney);
        tvAddress=findViewById(R.id.tvAddress);
        tvReMark=findViewById(R.id.tvReMark);
        tvOrdernum=findViewById(R.id.tvOrdernum);
        tvOrderCreateTime=findViewById(R.id.tvOrderCreateTime);
        tvName=findViewById(R.id.tvName);
        tvCallBuyer.setOnClickListener(this::onClick);
        tvCheckingComment.setOnClickListener(this::onClick);
        ivBack.setOnClickListener(this::onClick);
        tvCopy.setOnClickListener(this::onClick);
        if (model!=null){
            setView();
        }
    }

    public void setView(){
        tvName.setText(model.getReceiveName());
        tvPhone.setText(model.getReceivePhone());
        tvAddress.setText("地址："+model.getReceiveAddress());
        tvOrdernum.setText(model.getOrderNumber());
        tvOrderCreateTime.setText(model.getCreateTime());
        tvPayType.setText(model.getPayWay());
        tvPayTime.setText(model.getPayTime());
        tvReMark.setText("备注："+model.getRemark());
        tvColoudGold.setText("-¥"+model.getDiscount());
        tvGoodsMoney.setText(model.getTotalMoney()+"");
        tvTotalMoney.setText("¥"+model.getActuallyMoney());


        adapter=new OrderListChild_Adapter(model.getOrderDetailList(),1);
        rcView.setLayoutManager(new LinearLayoutManager(StoreOrderFinish.this));
        rcView.setAdapter(adapter);
    }
//
//    //获取订单详情
//    public void getData(){
//        Map<String, String> map = new HashMap<>();
//        map.put("orderNum", orderNum);
//        map.put("userId",getUserId(getContext()));
//        new HttpRequest(getContext()).doPost(UrlConstants.goodsOrder_orderDetail, null, map, OrderDetail.class, new HttpStringCallBack() {
//            @RequiresApi(api = Build.VERSION_CODES.M)
//            @Override
//            public void onSuccess(Object result) {
//                if (result instanceof OrderDetail) {
//                    model=(OrderDetail)result;
//                    setView();
//                }
//            }
//
//            @Override
//            public void onFailure(int code, String msg) {
//                Toast.makeText(cn.xof.xyms.ui.order.activity.ActivityOrderUnPay.this,msg,Toast.LENGTH_SHORT).show();
//            }
//        });
//    }
//
//    public void setView(){
//        adapter=new Order_Item_Adapter(model.getData().getOrderDetailList());
//        manager=new LinearLayoutManager(getContext());
//        rcView.setLayoutManager(manager);
//        rcView.setAdapter(adapter);
//
//        tvGoodsMoney.setText("￥"+model.getData().getPrice());
//        tvOrdernum.setText(model.getData().getOrderNum());
//        tvOrderCreateTime.setText(model.getData().getCreateTime());
//        tvPayTime.setText(model.getData().getPayTime());
//        tvAddress.setText(model.getData().getAddress());
//        tvName.setText(model.getData().getName());
//        tvPhone.setText(model.getData().getMobile());
//        tvFreight.setText("￥"+model.getData().getFreight());
//        tvTotalMoney.setText("￥"+model.getData().getRealPrice());
//        switch (model.getData().getPayType()){
//            case 1:
//                tvPayType.setText("支付宝");
//                break;
//            case 2:
//                tvPayType.setText("微信");
//                break;
//            case 3:
//                tvPayType.setText("Apple Pay");
//                break;
//            case 4:
//                tvPayType.setText("积分兑换");
//                break;
//            default:
//                tvPayType.setText("微信");
//                break;
//        }
//    }
//


    @Override
    protected void initData() {
        //getData();
    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }


    @Override
    protected void onPause() {
//        if(!EventBus.getDefault().isRegistered(this)){//加上判断
//            EventBus.getDefault().unregister(this);
//        }
        super.onPause();
    }

}
